package com.moorror.cola.strategy.plugin.markers;

import com.intellij.ide.util.PsiElementListCellRenderer;
import com.intellij.openapi.util.NlsSafe;
import com.intellij.openapi.util.NotNullLazyValue;
import com.intellij.psi.PsiElement;
import com.moorror.cola.strategy.plugin.bean.PsiElementWrapper;
import org.apache.commons.collections.CollectionUtils;
import org.jetbrains.annotations.Nullable;

import javax.swing.*;
import java.util.List;

import static com.intellij.openapi.util.Iconable.ICON_FLAG_VISIBILITY;

/**
 * @Author jiyanghuang
 * @Date 2022/3/14 11:07 PM
 */
public class ColaCellRenderer extends PsiElementListCellRenderer<PsiElement> {

    private NotNullLazyValue<List<PsiElementWrapper>> elements;

    public ColaCellRenderer(NotNullLazyValue<List<PsiElementWrapper>> elements) {
        this.elements = elements;
    }

    /**
     * 左边文字
     * @param element
     * @return
     */
    @Override
    public @NlsSafe String getElementText(PsiElement element) {
        return firstMatch(element).renderText();
    }

    /**
     * 右边文字
     * @param element
     * @param name
     * @return
     */
    @Override
    protected @Nullable
    @NlsSafe String getContainerText(PsiElement element, String name) {
        return firstMatch(element).renderContainerText();
    }

    @Override
    protected int getIconFlags() {
        return ICON_FLAG_VISIBILITY;
    }

    /**
     * 直接使用原生的 图片就可以了
     * @param element
     * @return
     */
    @Override
    protected Icon getIcon(PsiElement element) {
        return firstMatch(element).getIcon();
    }

    private PsiElementWrapper firstMatch(PsiElement element){
        if(CollectionUtils.isNotEmpty(elements.getValue())){
            for(PsiElementWrapper psiElementWrapper : elements.getValue()) {
                if(psiElementWrapper.getElement() == element){
                    return psiElementWrapper;
                }
            }
        }
        return null;
    }
}
